import { defineStore } from 'pinia'
import { ref } from 'vue'
import { menuAPI } from '@/api/menu'
import { ElMessage } from 'element-plus'

export const useMenuStore = defineStore('menu', () => {
  // 状态
  const menuTree = ref([])
  const permissions = ref([])
  const roles = ref([])
  const loading = ref(false)
  const collapsed = ref(false)

  // 获取当前用户菜单
  const getCurrentUserMenu = async () => {
    try {
      loading.value = true
      const result = await menuAPI.getCurrentUserMenu()

      if (result.success) {
        const { menuTree: tree, permissions: perms, roles: userRoles } = result.data

        // 更新状态
        menuTree.value = tree || []
        permissions.value = perms || []
        roles.value = userRoles || []

        return { success: true, data: result.data }
      } else {
        ElMessage.error(result.message || '获取菜单失败')
        return { success: false, message: result.message }
      }
    } catch (error) {
      console.error('获取菜单失败:', error)
      ElMessage.error('获取菜单失败，请稍后重试')
      return { success: false, message: '获取菜单失败' }
    } finally {
      loading.value = false
    }
  }

  // 检查权限
  const hasPermission = (permissionCode) => {
    return permissions.value.includes(permissionCode)
  }

  // 检查是否有任意一个权限
  const hasAnyPermission = (...permissionCodes) => {
    return permissionCodes.some(code => permissions.value.includes(code))
  }

  // 检查是否有所有权限
  const hasAllPermissions = (...permissionCodes) => {
    return permissionCodes.every(code => permissions.value.includes(code))
  }

  // 检查角色
  const hasRole = (roleCode) => {
    return roles.value.includes(roleCode)
  }

  // 切换侧边栏折叠状态
  const toggleCollapsed = () => {
    collapsed.value = !collapsed.value
  }

  // 清除菜单数据
  const clearMenu = () => {
    menuTree.value = []
    permissions.value = []
    roles.value = []
  }

  // 递归查找菜单项
  const findMenuItem = (tree, path) => {
    for (const item of tree) {
      if (item.path === path) {
        return item
      }
      if (item.children && item.children.length > 0) {
        const found = findMenuItem(item.children, path)
        if (found) return found
      }
    }
    return null
  }

  // 获取面包屑导航
  const getBreadcrumb = (currentPath) => {
    const breadcrumb = []

    const findPath = (tree, targetPath, path = []) => {
      for (const item of tree) {
        const currentBreadcrumb = [...path, { name: item.name, path: item.path }]

        if (item.path === targetPath) {
          breadcrumb.push(...currentBreadcrumb)
          return true
        }

        if (item.children && item.children.length > 0) {
          if (findPath(item.children, targetPath, currentBreadcrumb)) {
            return true
          }
        }
      }
      return false
    }

    findPath(menuTree.value, currentPath)
    return breadcrumb
  }

  return {
    // 状态
    menuTree,
    permissions,
    roles,
    loading,
    collapsed,

    // 方法
    getCurrentUserMenu,
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    hasRole,
    toggleCollapsed,
    clearMenu,
    findMenuItem,
    getBreadcrumb
  }
})